﻿using Microsoft.EntityFrameworkCore;
using PHMEE.EntityFrameworkCore;
using PHMEE.Migrations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace PHMEE
{
    public class SensorRepository : EfCoreRepository<PHMEEDbContext, Sensor, Guid>, ISensorRepository
    {
        public SensorRepository(IDbContextProvider<PHMEEDbContext> dbContextProvider) : base(dbContextProvider)
        {

        }
        /// <summary>
        /// 批量插入数据
        /// </summary>
        /// <param name="sensors"></param>
        /// <param name="autoSave"></param>
        /// <returns></returns>
        public async Task InsertManyAsync(List<Sensor> sensors, bool autoSave = false)
        {
            var dbContext = await GetDbContextAsync();
            await dbContext.Sensors.AddRangeAsync(sensors);

            if (autoSave)
            {
                await dbContext.SaveChangesAsync();
            }
        }
        /// <summary>
        /// 根据采集器编码删除所有传感器信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public async Task DeleteAsync(string code)
        {
            var dbContext = await GetDbContextAsync();
            var sensorsToDelete = dbContext.Sensors.Where(x => x.CollectCode == code);
            dbContext.Sensors.RemoveRange(sensorsToDelete);
            await dbContext.SaveChangesAsync();
        }
    }
}
